package com.thinkersre.service;

import com.thinkersre.dao.MessageMapper;
import com.thinkersre.entity.Message;
import com.thinkersre.util.SensitiveFilter;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageService {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private SensitiveFilter sensitiveFilter;
    public List<Message> findConversations(int userId,int offset, int pageSize){
        return messageMapper.selectConversations(userId, offset, pageSize);
    }
    public int findConversationCount(int userId){
        return messageMapper.selectConversationCount(userId);
    }
    public List<Message> findLetters(String conversationId,int offset, int pageSize){
        return messageMapper.selectLetters(conversationId,offset,pageSize);
    }
    public int findLetterCount (String conversationId){
        return messageMapper.selectLetterCount(conversationId);
    }
    public int findLetterUnreadCount(int userId,String conversationId){
        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }
    public int addMessage(Message message){
        //防止恶意注册
        message.setContent(HtmlUtils.htmlEscape (message.getContent()));
        //过滤敏感词
        message.setContent(sensitiveFilter.Filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }

    public int readMesssge(List<Integer> ids){
//        改为已读状态
        return messageMapper.updateStatus(ids,1);
    }

    //通知类
    public Message findLatestNotice(int userId, String topic){
        return messageMapper.selectLatestNotice(userId,topic);
    }

    public int findNoticeCount(int userId, String topic){
        return messageMapper.selectNoticeCount(userId, topic);
    }
    public int findNoticeUnreadCount(int userId, String topic){
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }
    public List<Message> findNotices(int userId,String topic,int offset,int pageSize){
        return messageMapper.selectNotices(userId, topic, offset, pageSize);
    }






}
